/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "纹理.h"

#include <lodepng.h>
#include <文件/file_文件名数据.h>


/*S_纹理* f_创建纹理(E_绘图API 后端, ivec2 size, E_纹理格式 格式)
{
	S_纹理* t = 0;

	if (后端 == E_绘图API::E_Vulkan) {
		throw std::runtime_error("还没有创建 vulkan 纹理对象!");
	}
	else {
		//t = new S_OpenGL纹理();
	}

	//t->f_创建纹理(size, 格式);

	return t;
}

S_结构对象指针 f_创建纹理(ivec3 size, E_纹理格式 格式) {

	return S_结构对象指针();
}*/


S_纹理* f_fromFile创建纹理(S_设备环境& ctx, S_纹理采样器* sample, std::string path) {
	ubvec4* data_p = 0;
	uvec3 size = { 0,0, 1 };

	lodepng_decode_file((uint8**)&data_p, &size.x, &size.y, path.c_str(), LodePNGColorType::LCT_RGBA, 8);

	S_纹理* 纹理 = f_tex_创建图像纹理(ctx, size, E_纹理格式::e_tf_RGBA8U_U, E_纹理维度类型::e_2D);

	f_tex_填充纹理数据(纹理, (const uint8*)data_p, sizeof(ubvec4), size);
	f_tex_安装纹理采样器(纹理, sample);
	
	free(data_p);
	return 纹理;
}

S_纹理* f_fromFile创建纹理数组(S_设备环境& ctx, S_纹理采样器* sample, std::vector<std::string> path) {
	std::vector<ubvec4> data;

	uint32 num = path.size();
	uvec3 维度 = { 0,0, num };

	
	for (auto& fileName : path) {
		ubvec4* data_p = 0;
		uvec2 dim = { 0, 0 };

		auto err = lodepng_decode_file((uint8**)&data_p, &dim.x, &dim.y, fileName.c_str(), LodePNGColorType::LCT_RGBA, 8);

		维度.x = dim.x;
		维度.y = dim.y;

		uint64 size = dim.x * dim.y;
		uint64 offset = data.size();

		data.resize(offset + size);

		memcpy(&(data.data()[offset]), data_p, size * sizeof(ubvec4));

		if (!err) {
			free(data_p);
		}
	}

	S_纹理* 纹理 = f_tex_创建图像纹理(ctx, 维度, E_纹理格式::e_tf_RGBA8U_U, E_纹理维度类型::e_2D_Array);

	f_tex_填充纹理数据(纹理, (const uint8*)data.data(), sizeof(ubvec4), 维度);
	f_tex_安装纹理采样器(纹理, sample);

	return 纹理;
}


//void f_tex_从文件填充纹理(S_纹理* tex, std::string path, S_结构对象指针 命令缓存) {
//	auto 后缀 = f_file_文件扩展名(path);
//
//	if (后缀 == ".png") {
//
//	}
//	else if (后缀 == ".jpg") {
//
//	}
//	else if (后缀 == ".tga") {
//
//	}
//	else if (后缀 == ".psd") {
//
//	}
//	else if (后缀 == ".hdr") {
//
//	}
//	else if (后缀 == ".bmp") {
//
//	}
//
//	ubvec4* data_p = 0;
//	uvec3 维度 = { 0,0, 1 };
//
//	lodepng_decode_file((uint8**)&data_p, &维度.x, &维度.y, path.c_str(), LodePNGColorType::LCT_RGBA, 8);
//
//	f_tex_修改大小(tex, 维度);
//	f_tex_填充纹理数据(tex, (const uint8*)data_p, sizeof(ubvec4), 维度, 命令缓存);
//
//	free(data_p);
//}

void f_tex_保存到文件(S_纹理* tex, std::string path, S_结构对象指针 命令缓存) {
	uint32 bitdepth = 8;
	uint32 通道数量 = 1;
	LodePNGColorType type = LodePNGColorType::LCT_RGBA;
	switch (tex->m_格式) {
	case e_tf_R8U_U: type = LodePNGColorType::LCT_GREY; bitdepth = 8; break;
	case e_tf_RG8U_U: type = LodePNGColorType::LCT_GREY; 通道数量 = 2; bitdepth = 8; break;
	case e_tf_RGB8U_U: type = LodePNGColorType::LCT_RGB; 通道数量 = 3; bitdepth = 8; break;
	case e_tf_RGBA8U_U: type = LodePNGColorType::LCT_RGBA; 通道数量 = 4; bitdepth = 8; break;

	case e_tf_R32: type = LodePNGColorType::LCT_GREY; bitdepth = 32; break;
	case e_tf_RG32: type = LodePNGColorType::LCT_GREY; 通道数量 = 2; bitdepth = 32; break;
	case e_tf_RGB32: type = LodePNGColorType::LCT_RGB; 通道数量 = 3; bitdepth = 32; break;
	case e_tf_RGBA32: type = LodePNGColorType::LCT_RGBA; 通道数量 = 4; bitdepth = 32; break;
		
	case e_tf_R16U: type = LodePNGColorType::LCT_GREY; bitdepth = 16; break;
	case e_tf_RG16U: type = LodePNGColorType::LCT_GREY; 通道数量 = 2; bitdepth = 16; break;
	case e_tf_RGB16U: type = LodePNGColorType::LCT_RGB; 通道数量 = 3; bitdepth = 16; break;
	case e_tf_RGBA16U: type = LodePNGColorType::LCT_RGBA; 通道数量 = 4; bitdepth = 16; break;
		

	default:
		break;
	}

	//S_板载缓存* 临时缓存 = f_buf_create板载缓存(*tex->m_Ctx, 通道数量 * bitdepth, E_板载缓存类型::e_TBOs, tex->m_Size.x * tex->m_Size.y);
	S_板载缓存* 临时缓存 = f_buf_create板载缓存(*tex->m_Ctx, 通道数量 * bitdepth, E_板载缓存类型::e_SSBO, tex->m_Size.x * tex->m_Size.y);

	f_tex_读取纹理数据(tex, 临时缓存, tex->m_命令缓存);
	临时缓存->f_map();
	auto* pix = (uint8*)(临时缓存->m_mappedPtr);
	//for (uint32 x = 0; x < tex->m_Size.x; ++x) {
	//	std::cout<< int32(pix[x*4]) << int32(pix[x*4+1]) << int32(pix[x*4+2]) << int32(pix[x * 4 + 3]) << std::endl;
	//}
	lodepng_encode_file(path.c_str(), (uint8*)(临时缓存->m_mappedPtr), tex->m_Size.x, tex->m_Size.y, type, bitdepth);
	临时缓存->f_unmap();

	f_buf_release板载缓存(临时缓存);
	//delete 临时缓存;

}





